import os
import logging

logger = logging.getLogger(__name__)

def get_review_path(title: str) -> str:
    """
    获取综述文件路径
    """
    review_dir = os.path.join(os.getcwd(), "reviews")
    if not os.path.exists(review_dir):
        os.makedirs(review_dir)
    return os.path.join(review_dir, f"{title.replace(' ', '_')}.md")

def save_review(title: str, content: str, citations: List[Dict]) -> None:
    """
    保存综述到文件
    """
    file_path = get_review_path(title)
    try:
        with open(file_path, 'w', encoding='utf-8') as f:
            f.write(f"# {title}\n\n")
            f.write(content)
            f.write("\n\n## References\n\n")
            for citation in citations:
                f.write(f"[{citation['number']}] {citation['title']}. "
                       f"{citation['venue']}, {citation['year']}, chunk {citation['chunk_id']}\n")
        logger.info(f"综述已保存到: {file_path}")
    except Exception as e:
        logger.error(f"保存综述时出错: {str(e)}")
